<template>
  <MainSlot :show-tab="false" :show-btn-box="false">
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" @search="(e) => {
          (searchlist = { ...e }), (currentPage = 1), getTableDataList();
        }
        " />
    </template>
    <template v-slot:table>
      <TableData :tableThead="tableThead" :dataList="dataList" @selection-change="selectionChange">
        <template v-slot:materialNo="{ row }">
          <span>{{ row.material.materialName }}</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination  @size-Change="handleSizeChange" :size="pageSize"  @current-change="handleCurrentChange" :page="currentPage" :total="total">
      </Pagination>
    </template>
  </MainSlot>
</template>

<script>
import MainSlot from "@/components/MainSlot";
import Pagination from "@/components/Pagination";
import SearchForm from "@/components/SearchForm";
import TableData from "@/components/TableData";
import BtnSlot from "@/components/BtnSlot";
import EnumSlot from "@/components/EnumSlot";

import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";

import batchApi from "@/api/livestock-manage/storage-manage/material-batch";
import materialApi from "@/api/basic-info-manage/agr-source-base-Manage";
import personApi from "@/api/livestock-manage/personnel-cost-mansge/staff-manage";

export default {
  name: "SheepInformation",
  components: {
    SearchForm,
    TableData,
    BtnSlot,
    EnumSlot,
    Pagination,
    MainSlot,
  },
  mixins: [mymixin],
  data() {
    return {
      total: 100,
      selectIndex: 0,
      currentPage: 1,
      dataList: [],
      tableThead: [
        { isShow: true, label: "物料", slotName: "materialNo" },
        { isShow: true, label: "行号", prop: "batchNo" },
        { isShow: true, label: "批号", prop: "lotNumber" },
        { isShow: true, label: "批次生成时间", prop: "batchtime" },
        { isShow: true, label: "生产日期", prop: "produceDate" },
        { isShow: true, label: "失效日期", prop: "expirationDate" },
        { isShow: true, label: "备注", prop: "remarks" },
      ],
      formItemList: [
        {
          label: "物料名称",
          type: "select",
          selectOptions: [],
          param: "materialNo",
          defaultSelect: "",
          multiple: false,
          isShow: true,
        },
        {
          label: "行号",
          type: "input",
          param: "batchNo",
          multiple: false,
          isShow: true,
        },
        {
          label: "批号",
          type: "input",
          param: "lotNumber",
          multiple: false,
          isShow: true,
        },
      ],
      materialList: [], //材料数据
      // =================下面疑似无用数据
      searchlist: {},
      formConfig: [
        {
          label: "物料名称",
          prop: "materialName",
          type: "select",
          options: [],
          optionKey: { label: "materialName", value: "materialNo" },
        },
        {
          label: "行号",
          prop: "batchNo",
          type: "input",
        },
        {
          label: "批号",
          prop: "lotNumber",
          type: "input",
        },
      ],
      selectedStaffList: [],
      // selectValue 用于传递select的编号
      formDatalists: [
        {
          type: "Select", // input Select Checkbox TimePicker cascader
          prop: "materialNo",
          porpName: "materialName",
          porpObj: "material",
          selectValue: "materialNo",
          typeObj: {
            list: [],
          },
          name: "物料",
          required: true,
        },
        {
          type: "input", // input Select Checkbox TimePicker
          prop: "batchNo",
          selectValue: "",
          typeObj: {},
          name: "行号",
          required: true,
        },
        {
          type: "input", // input Select Checkbox TimePicker
          prop: "lotNumber",
          selectValue: "",
          typeObj: {},
          name: "批号",
          required: true,
        },
        {
          type: "Select", // input Select Checkbox TimePicker
          prop: "batchtime",
          selectValue: "batchtime",
          typeObj: {},
          name: "批次生成时间",
          required: true,
          notSearch: true,
        },
        {
          type: "input", // input Select Checkbox TimePicker
          prop: "produceDate",
          selectValue: "",
          typeObj: {},
          name: "生产日期",
          required: true,
          notSearch: true,
        },
        {
          type: "Select", // input Select Checkbox TimePicker
          prop: "expirationDate",
          selectValue: "expirationDate",
          typeObj: {},
          name: "失效日期",
          required: true,
          notSearch: true,
        },
        {
          type: "input", // input Select Checkbox TimePicker
          prop: "remarks",
          selectValue: "",
          typeObj: {},
          name: "备注",
          required: false,
          notSearch: true,
        },
      ],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  created() {
    this.search();
    this.getPerson();
    this.getMaterialType();
  },
  methods: {
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = [];
      list.forEach((item, index) => {
        if (item.param == "materialNo") {
          let selectOptions = this.materialList.map((data) => {
            return {
              text: data.materialName,
              value: data.materialNo,
            };
          });
          item.selectOptions = selectOptions;
        }
      });
      this.$nextTick(() => {
        this.$set(this, "formItemList", list);

      });
    },
    async getMaterialType() {
      let res = await materialApi.getqueryList({});
      this.materialList = res.data || [];
      this.setFormOptions();
    },
    // 获取记录人列表
    getPerson() {
      personApi.getStaffListByCondition().then((res) => {
        if (res.code === 1) {
          let list = [];
          this.personList = res.data || [];
          res.data.forEach((item) => {
            list.push({
              name: item.person.name,
              value: item.personNo,
            });
          });
          this.formDatalists.forEach((item, index) => {
            if (item.prop === "personName") {
              this.$set(this.formDatalists[index].typeObj, "list", list);
            }
          });
        }
      });
    },
    // 多选方法
    selectionChange(val) {
      this.selectionList = val || [];
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleSizeChange(size){
      this.pageSize = size;
      this.getTableDataList();
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchlist,
          organizationNo: sessionStorage.organizationNo,
          breedTypeNo: sessionStorage.breedTypeNo,
        },
      };
      batchApi.pageQuery(params).then((res) => {
        this.loading = false;
        let data = res.data;
        // 赋值总条数
        if (data) {
          // 赋值数组
          this.dataList = res.data.content;
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.getTableDataList();
          }
        }
      });
    },
    handleSelectionChange(list) {
      this.selectedStaffList = [];
      this.selectedStaffList = list.map((item) => item.batchNo);
      if (this.selectedStaffList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      batchApi
        .numberBatchDel({ noList: this.selectedStaffList })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
          } else {
            this.$message.warning(res.msg || "删除失败");
          }
        });
    },
    handleDelete(val) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          batchApi.numberDel(val).then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.search();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => { });
    },
    add() {
      this.formData = {};
      this.nowChanceStau = "add";
      this.titleText = "添加供应商";
      this.addActive = false;
      this.delActive = false;
      this.dialogFormVisible = true;
    },
    edit(row) {
      this.formData = {
        ...row,
        materialNo: row.material.materialNo,
      };
      console.log(this.formData);
      this.nowChanceStau = "edit";
      this.titleText = "修改供应商";
      this.dialogFormVisible = true;
      this.addActive = true;
      this.delActive = false;
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let formData = { ...this.formData };
            batchApi.edit(formData).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.dialogFormVisible = false;
                this.search();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let person = { ...this.formData };
            batchApi.add(person).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.dialogFormVisible = false;
                this.search();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
@import "~@/styles/variables.scss";
</style>
